package com.aat.dxfy.cmd;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.filechooser.FileNameExtensionFilter;

public class UseFrameStartJar extends JFrame implements ActionListener {
	// 参数
	private static final long serialVersionUID = 1L;
	private int x = Toolkit.getDefaultToolkit().getScreenSize().width;
	private int y = Toolkit.getDefaultToolkit().getScreenSize().height;
	private int w = x / 2;
	private int h = y / 2;
	private JButton choice, execute, stop;
	private JTextArea text;
	private JTextField jarFile;
	// UI
	private LookAndFeelInfo[] look;

	private Thread t;
	private Process p;
	private BufferedInputStream bis;

	public UseFrameStartJar() {
		super("Jframe Start Jar");
		// 添加组件
		this.setBounds(w / 2, h / 2, w, h);
		this.setLayout(null);
		JLabel jl1 = new JLabel("选择Jar包");
		jl1.setBounds(10, 10, w / 6, 30);
		jarFile = new JTextField("");
		jarFile.setBounds(w / 6 + 10, 10, w / 3, 30);

		choice = new JButton("选择");
		choice.setBounds(w / 2 + 10, 10, w / 8, 30);
		execute = new JButton("启动");
		execute.setBounds(5 * w / 8 + 10, 10, w / 8, 30);
		stop = new JButton("停止");
		stop.setBounds(3 * w / 4 + 10, 10, w / 8, 30);

		choice.addActionListener(this);
		execute.addActionListener(this);
		stop.addActionListener(this);

		text = new JTextArea();
		text.setEditable(false);
		text.setLineWrap(true);
		JScrollPane scroll = new JScrollPane(text);
		scroll.setBounds(10, 50, w - 30, h - 90);

		//

		//

		this.add(jl1);
		this.add(jarFile);
		this.add(choice);
		this.add(execute);
		this.add(stop);
		this.add(scroll);

		try {
			look = UIManager.getInstalledLookAndFeels();
			UIManager.setLookAndFeel(look[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		// this.setUndecorated(true);
		this.setVisible(true);
		this.setResizable(false);// 不放大
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void runJar(String jarFilePath) {
		Runtime r = Runtime.getRuntime();
		try {
			p = r.exec("java -jar " + jarFilePath);
			bis = new BufferedInputStream(p.getInputStream());
			byte[] bs = new byte[1024];
			int b = -1;
			while ((b = bis.read(bs)) != -1) {
				String s = new String(bs, 0, b) + "\n";
				text.append(s);
				text.setCaretPosition(text.getText().length());
			}
		} catch (IOException e) {
		}finally {
			p.destroy();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == choice) {
			JFileChooser jfc = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("可执行jar包", "jar");
			jfc.setFileFilter(filter);
			jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			int returnVal = jfc.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = jfc.getSelectedFile();
				if (file.isFile()) {
					String fp = file.getAbsolutePath();
					if (fp.endsWith("jar")) {
						jarFile.setText(fp);
					} else {
						jarFile.setText("");
					}
				} else {
					jarFile.setText("");
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择可执行的jar文件", "提示消息", JOptionPane.INFORMATION_MESSAGE);
			}

		} else if (e.getSource() == execute) {
			final String jarFilePath = jarFile.getText();
			if (jarFilePath != null && jarFilePath.length() > 0) {
				this.setTitle("Running " + jarFilePath);
				if (t == null) {
					t = new Thread() {
						@Override
						public void run() {
							runJar(jarFilePath);
						}
					};
					t.setDaemon(true);
					t.start();
				} else {

				}

			}
		} else if (e.getSource() == stop) {
			if (t != null) {
				t.interrupt();
				t = null;
				try {
					p.destroy();
					bis.close();
					Runtime.getRuntime().gc();
				} catch (IOException e1) {
				}
			}
		}
	}

	public static void main(String[] args) {
		new UseFrameStartJar();
	}

}
